﻿using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System;
using System.Diagnostics;

public class OsxWindowsTool
{
    public static string SaveFilePath(string dirpath, string filename, string ext)
    {
        return SFB.StandaloneFileBrowser.SaveFilePanel("save", dirpath, filename, ext);
    }

    public static string OpenFileFolderPath(string title)
    {
        var folders = SFB.StandaloneFileBrowser.OpenFolderPanel(title, "/Users", false);
        if (folders == null || folders.Length == 0)
        {
            return null;
        }
        return folders[0];
    }

    public static string OpenFilePath(string title, string ext)
    {
        var files = SFB.StandaloneFileBrowser.OpenFilePanel(title, "/Users", ext, false);
        if (files == null || files.Length == 0)
        {
            return null;
        }
        return files[0];
    }


    public static void CopyFile(string filePath, string exportPath)
    {
        if (System.IO.File.Exists(filePath))
        {
            System.IO.File.Copy(filePath, exportPath, true);
        }
    }

    /// <summary>
    /// 复制文件夹中的所有文件夹与文件到另一个文件夹
    public static void CopyDir(string sourcePath, string destPath)
    {
        if (Directory.Exists(sourcePath))
        {
            if (!Directory.Exists(destPath))
            {
                //目标目录不存在则创建
                try
                {
                    Directory.CreateDirectory(destPath);
                }
                catch (Exception ex)
                {
                    throw new Exception("创建目标目录失败：" + ex.Message);
                }
            }
            //获得源文件下所有文件
            List<string> files = new List<string>(Directory.GetFiles(sourcePath));
            files.ForEach(c =>
            {
                string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                File.Copy(c, destFile, true);//覆盖模式
            });
            //获得源文件下所有目录文件
            List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));
            folders.ForEach(c =>
            {
                string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                //采用递归的方法实现
                CopyDir(c, destDir);
            });
        }
        else
        {
            throw new DirectoryNotFoundException("源目录不存在！");
        }
    }

    public static void OpenFile(string filePath)
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.FileName = "open";
        process.StartInfo.Arguments = filePath;
        process.Start();
    }

    public static void OpenSelectFolder(string folderPath)
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.FileName = "open";
        process.StartInfo.Arguments = folderPath;
        process.Start();
    }

    public static void Execute(string filePath)
    {
        if (filePath.EndsWith(".py"))
        {
           UFrame.MacTool.Bash("python " + filePath, Application.dataPath, true);
        }
    }
}
